package com.ruoyi.merchant.controller;

import com.ruoyi.merchant.utils.WebSocketServer;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ysq
 * @Date: 2024/09/13/15:33
 * @Description:
 */
@RestController("/message")
public class WebController {

    @PostMapping("/send")
    public ResponseEntity<String> sendMessage(@RequestParam String userId, @RequestParam String message) {
        try {
            WebSocketServer.sendInfo(message, userId);
            return ResponseEntity.ok("Message sent successfully to user: " + userId);
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to send message: " + e.getMessage());
        }
    }
}
